<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 
 * @copyright (c) 2013
 * @author Babak Taraghi 'babak.taraghi@gmail.com'
 * @license Apache License 2.0
 * @version 1.0
 * @desc A simple model to handle simple queries
 */
class Simple_Data_Model extends CI_Model {
	
	
	
	/**
	 * 
	 * @desc String - Current physical table name
	 */
	private $tableName = NULL;
	
	
	
	/**
	 * 
	 * @desc String - Table primary key field name
	 */
	private $idFIELD = NULL;
	
	
	
	/**
	 * 
	 * Initialize the object
	 * @param String $tableName
	 * @param String $idFIELD Table primary key field name
	 */
	public function init($tableName,$idFIELD){
		$this->tableName = $tableName;
		$this->idFIELD = $idFIELD;
	}
	
	
	
	/**
	 * 
	 * @desc Will return the total number of records in table
	 * @param $where this is the where clause in query, array or string
	 */
	public function getTotalRecordsNumber($where=""){
		if($where != ""){
			$this->db->where($where);
		}
		$this->db->from($this->tableName);
		return $this->db->count_all_results();
	}
	
	
	
	
	/**
	 * 
	 * @desc This the same as getTotalRecordsNumber but it will use 'or like' query
	 * @param $where this is the where clause in query can be array or string
	 */
	public function getSearchedRecordsNumber($where){
		$this->db->or_like($where);
		$this->db->from($this->tableName);
		return $this->db->count_all_results();
	}
	
	
	
	
	/**
	 * 
	 * @desc This will return one record of the table
	 * @param $keyFieldName this is the column name in where clause
	 * @param $keyFieldValue this is the value for the specified column
	 */
	public function getRecord($keyFieldName,$keyFieldValue){
		$query = $this->db->get_where($this->tableName, array($keyFieldName => $keyFieldValue));
		return $query->row();
	}
	
	
	
	
	/**
	 * 
	 * @desc Something like getRecords but it will generate 'or like' query
	 * @param $where this is the where clause can be string or array
	 * @param $from this is starting from record number
	 * @param $rows this is the finishing record number
	 * @param $sortBy records will be sorted descending using this column
	 */
	public function searchRecords($where, $from=0, $rows=0, $sortBy=NULL, $sortType=NULL){
		if(!is_null($sortBy) && !is_null($sortType))
			$this->db->order_by($sortBy,$sortType);
			
		$this->db->or_like($where);
		if($from == 0 && $rows == 0){
			$query = $this->db->get($this->tableName);
		}else{
			$query = $this->db->get($this->tableName,$rows,$from);
		}
		return $query->result();
	}
	
	
	
	
	
	/**
	 * 
	 * @desc This will return records of table
	 * @param $where this is the where clause can be string or array
	 * @param $from this is starting from record number
	 * @param $rows this is the finishing record number
	 * @param $sortBy records will be sorted descending using this column
	 */
	public function getRecords($where=NULL, $from=0, $rows=0, $sortBy=NULL, $sortType=NULL){
		if(!is_null($sortBy) && !is_null($sortType))
			$this->db->order_by($sortBy,$sortType);
			
		if(!is_null($where)){
			if(is_string($where))
				$this->db->where($where,NULL,FALSE);
			else
				$this->db->where($where);
		}
		if($from == 0 && $rows == 0){
			$query = $this->db->get($this->tableName);
		}else{
			$query = $this->db->get($this->tableName,$rows,$from);
		}
		return $query->result();
	}
	
	
	
	
	
	/**
	 * 
	 * @desc This will insert a record in table
	 * @param $fieldsValues array of columns and insertable values
	 * @return Integer last insert id on success and false on failure
	 */
	public function insert($fieldsValues){
		if($this->db->insert($this->tableName, $fieldsValues)){
			return $this->db->insert_id();
		}
		return FALSE;
	}
	
	
	
	
	
	/**
	 * 
	 * @desc This will update the fields in specified record
	 * @param $id the id value of the record which should update
	 * @param $fieldsValues array of columns and updatable values
	 */
	public function update($id,$fieldsValues){
		return $this->db->update($this->tableName, $fieldsValues, array('id' => $id));
	}
	
	
	
	
	/**
	 * 
	 * @desc This will delete records of table with the specified id
	 * @param $ids array of ids which have to be delete
	 */
	public function delete($ids){
		$this->db->where_in("id",$ids);
		return $this->db->delete($this->tableName);
	}
	
}